package git.soulbgm.service.impl;

import git.soulbgm.mapper.ImUserMapper;
import git.soulbgm.pojo.FriendListing;
import git.soulbgm.pojo.ImUser;
import git.soulbgm.service.FriendListingService;
import git.soulbgm.service.ImUserService;
import git.soulbgm.utils.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;

/**
 * @author 贺瑞杰
 * @version V1.0
 * @date 2019-04-10 17:44
 * @description
 */
@Service
public class ImUserServiceImpl extends BaseServiceImpl<ImUserMapper,ImUser> implements ImUserService {

    @Autowired
    private FriendListingService friendListingService;

    @Override
    public HashMap<String, Object> login(String username, String password) {
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        ImUser user = ImUser.builder()
                .username(username)
                .password(password)
                .build();
        user = mapper.selectOne(user);
        if (user != null) {
            user.setPassword("");
        }
        List<FriendListing> friendListing = friendListingService.findByModel(FriendListing.builder().userId(user.getId()).build());
        return MapUtil.newHashMap(new String[]{"user", "friendListing"}, new Object[]{user, friendListing});
    }

    @Override
    public ImUser register(ImUser user) {
        String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(password);
        if (save(user)) {
            return user;
        } else {
            return null;
        }
    }


}
